package com.health.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.context.BaseContext;
import com.health.entity.Department;
import com.health.entity.Doctor;
import com.health.entity.Order;
import com.health.entity.Patient;
import com.health.mapper.DepartmentMapper;
import com.health.mapper.DoctorMapper;
import com.health.mapper.OrderMapper;
import com.health.mapper.PatientMapper;
import com.health.result.PageResult;
import com.health.service.OrderService;
import com.health.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private PatientMapper patientMapper;


    //预约
    @Override
    public OrderVO Order(Long doctorId,Long deptId,Long patientId) {
        OrderVO o = new OrderVO();
        //查询医生
        Doctor D = doctorMapper.getById(doctorId);
        o.setDoctorName(D.getName());
        o.setDoctorTags(D.getTags());
        o.setDoctorId(doctorId);
        //诊费
        o.setFee(D.getFee());
        //科室
        Department department =  departmentMapper.getByDepartId(deptId);
        o.setDeptName(department.getName());
        //就诊人
        Patient p =  patientMapper.getByPatientId(patientId);
        o.setPatientName(p.getName());
        o.setPatientCardNumber(p.getCardNumber());

        //医院地址和医院名
        o.setHospitalName("医院名");
        o.setHospitalAddress("医院地址");

        Long UserID = BaseContext.getCurrentId();
        log.info("////////////////==========/////////UserID:{}",UserID);

        return o;
    }

    //提交预约
    @Override
    public Long save(Order order) {
        order.setState(1);
        orderMapper.save(order);
        Long id = order.getId();

        return id;
    }

    @Override
    public OrderVO getById(Long orderId) {
        Order order = orderMapper.getById(orderId);
        OrderVO o = new OrderVO();
        BeanUtils.copyProperties(order,o);

        //查询医生
        Doctor D = doctorMapper.getById(order.getDoctorId());
        o.setDoctorName(D.getName());
        o.setDoctorTags(D.getTags());
        //科室
        Department department =  departmentMapper.getByDepartId(order.getDeptId());
        o.setDeptName(department.getName());
        //就诊人
        Patient p =  patientMapper.getByPatientId(order.getPatientId());
        o.setPatientName(p.getName());
        o.setPatientCardNumber(p.getCardNumber());

        //医院地址和医院名
        o.setHospitalName("医院名");
        o.setHospitalAddress("医院地址");

        return o;
    }

    @Override
    public List<OrderVO> list(Long patientId, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        Page<OrderVO> voPage = (Page<OrderVO>) orderMapper.list(patientId);
        List<Doctor> doctor = doctorMapper.getAll();
        //就诊人信息
        Patient patient = patientMapper.getByPatientId(patientId);
        //医生标签
        Map<Long, String> tagsList = doctor.stream().collect(Collectors.toMap(Doctor::getId, Doctor::getTags));
        //医生名
        Map<Long, String> doctorName = doctor.stream().collect(Collectors.toMap(Doctor::getId, Doctor::getName));
        //科室
        List<Department> department = departmentMapper.getAll();
        Map<Long, String> departmentList = department.stream().collect(Collectors.toMap(Department::getId, Department::getName));

        List<OrderVO> result = voPage.getResult();
        for (OrderVO vo : result) {
            vo.setPatientName(patient.getName());
            //医生标签
            vo.setDoctorTags(tagsList.get(vo.getDoctorId()));
            //卡号
            vo.setPatientCardNumber(patient.getCardNumber());
            //科室
            vo.setDeptName(departmentList.get(vo.getDeptId()));
            //医生名
            vo.setDoctorName(doctorName.get(vo.getDoctorId()));
        }
        return result;
//        return new PageResult(result);
    }
}
